package com.walter.blog.config;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.walter.aliyun.common.AliyunInfoConst;
import com.walter.blog.exception.ConfigNotFoundException;
import com.walter.db.mapper.ConfigMapper;
import com.walter.db.pojo.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 *
 * @author wuchenxi
 * @date 2021-06-20 08:45:16
 */
@Slf4j
@Configuration
public class AliyunConfig {

    @Resource
    private ConfigMapper configMapper;

    @PostConstruct
    public void init() {
        Config config = configMapper.selectByType(2);
        if (config == null) {
            throw new ConfigNotFoundException(2);
        }

        JSONObject jsonObject = JSONUtil.parseObj(config.getContent());
        AliyunInfoConst.host = jsonObject.getStr("host");
        AliyunInfoConst.bucketName = jsonObject.getStr("bucketName");
        AliyunInfoConst.endpoint = jsonObject.getStr("endpoint");
        AliyunInfoConst.accessKeyId = jsonObject.getStr("accessKeyId");
        AliyunInfoConst.accessKeySecret = jsonObject.getStr("accessKeySecret");

        log.info("阿里云配置加载完成！！！");
    }
}
